﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;
using System.Text;

namespace MyHelper
{
    public class DataList<T> where T : class
    {
        protected IEnumerable<T> _dataSource;
        protected DataListItem<T> _item;

        protected TextWriter _writer;
        protected ViewContext _context;
        protected ViewEngineCollection _engines;

        int _columns = 3;

        public int Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

        public DataList(IEnumerable<T> dataSource, TextWriter writer, ViewContext context)
        {
            _dataSource = dataSource;
            _item = new DataListItem<T>();

            _writer = writer;
            _context = context;

            _engines = ViewEngines.Engines;
        }

        public DataList<T> Item(Action<DataListItem<T>> itemBuilder)
        {
            itemBuilder(_item);

            return this;
        }

        public override string ToString()
        {
            StringBuilder sbOutput = new StringBuilder();
            StringWriter writer = new StringWriter(sbOutput);

            renderToWriter(writer);

            return sbOutput.ToString();
        }

        private void RenderText(string text)
        {
            _writer.Write(text);
        }

        private void renderToWriter(TextWriter writer)
        {
            // Temporarily using a stringbuilder for rendering the markup

            writer.Write("<table>");

            int cellIndex = 0;

            foreach (T item in _dataSource)
            {
                if (cellIndex == 0)
                {
                    writer.Write("<tr>");
                }

                writer.Write("<td");

                if (_item._itemAttributes != null && _item._itemAttributes.Count > 0)
                {
                    foreach (string key in _item._itemAttributes.Keys)
                    {
                        writer.Write(string.Format(" {0}=\"{1}\"", key, _item._itemAttributes[key]));
                    }
                }

                writer.Write(">");

                if (_item.CustomRenderer != null)
                {
                    _item.CustomRenderer(new RenderingContext(_writer, _context, _engines), item);
                }
                else
                {
                    // No render function
                }

                writer.Write("</td>");

                cellIndex++;

                if (cellIndex == _columns)
                {
                    cellIndex = 0;
                    writer.Write("</tr>");
                }
            }

            if (cellIndex != 0)
            {
                for (; cellIndex < _columns; cellIndex++)
                {
                    writer.Write("<td>&nbsp;</td>");
                }

                writer.Write("</tr>");
            }

            writer.Write("</table>");
        }


        public void Render()
        {
            renderToWriter(_writer);
        }
    }
}
